package br.cs.correspodente.janela;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.text.ParseException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import br.cs.correspodente.DAO.VendedorDAO;
import br.cs.correspodente.beans.Vendedor;
import br.cs.correspodente.factory.FacoryDAO;
import br.cs.correspodente.interfaces.IvendedorDAO;
import br.cs.correspodente.model.VendedorModel;
import br.cs.correspodente.modelo.ModeloTabelaBuscaVendedor;
import br.cs.correspondente.servicos.Visao;
import javax.swing.ButtonGroup;

public class DialogBuscaVendedor extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5704545457236626763L;
	private final JPanel contentPanel = new JPanel();
	private JTable jtVendedor;
	private JTextField txtNomeCNPJ;
	private ModeloTabelaBuscaVendedor modeloTabelaBuscaVendedor = new ModeloTabelaBuscaVendedor();
	private JRadioButton rdbtnNome;
	private JRadioButton rdbtnCpf;
	private JRadioButton rdbtnCnpj;
	private JFormattedTextField jftCpfBusca;
	private JLabel lblBusca;
	private Vendedor vendedor = new Vendedor();
	private final ButtonGroup buttonGroup = new ButtonGroup();



	public Vendedor getVendedor() {
		return vendedor;
	}

	public void setVendedor(Vendedor vendedor) {
		this.vendedor = vendedor;
	}

	/**
	 * Create the dialog.
	 */
	public DialogBuscaVendedor() {
		setTitle("Busca Vendedor");
		setModal(true);
		setBounds(100, 100, 700, 500);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		JScrollPane spVendedo = new JScrollPane();

		JPanel painelTp_Busca = new JPanel();
		painelTp_Busca.setBorder(new TitledBorder(null, "Buscar Por", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel painel_Busca = new JPanel();
		painel_Busca.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JButton btnSelecionar = new JButton("Selecionar");
		btnSelecionar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jtVendedor.getSelectedRow() < 0 ){
					Visao.showMsgErro("Nenhum Vendedor Selecionado","Aviso");
					return;
				}
				setVendedor(modeloTabelaBuscaVendedor.getVendedorAt(jtVendedor.getSelectedRow()));
				dispose();
			}
		});
		btnSelecionar.setIcon(new ImageIcon(DialogBuscaVendedor.class.getResource("/br/cs/correspodente/resorces/iconSalvar.png")));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
										.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_contentPanel.createSequentialGroup()
														.addComponent(painelTp_Busca, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(painel_Busca, GroupLayout.PREFERRED_SIZE, 462, GroupLayout.PREFERRED_SIZE))
														.addComponent(spVendedo, GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE))
														.addContainerGap())
														.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
																.addComponent(btnSelecionar)
																.addGap(272))))
				);
		gl_contentPanel.setVerticalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
										.addContainerGap()
										.addComponent(painelTp_Busca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPanel.createSequentialGroup()
												.addGap(25)
												.addComponent(painel_Busca, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)))
												.addGap(18)
												.addComponent(spVendedo, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(btnSelecionar)
												.addContainerGap(8, Short.MAX_VALUE))
				);
		painel_Busca.setLayout(null);

		lblBusca = new JLabel("Nome:");
		lblBusca.setBounds(6, 28, 42, 16);
		painel_Busca.add(lblBusca);

		jftCpfBusca = new JFormattedTextField();
		jftCpfBusca.setVisible(false);
		jftCpfBusca.setBounds(45, 22, 115, 28);
		try {
			jftCpfBusca.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		jftCpfBusca.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				zeraPreencheTableVendedor();
			}
		});
		painel_Busca.add(jftCpfBusca);

		txtNomeCNPJ = new JTextField();
		txtNomeCNPJ.setBounds(45, 22, 404, 28);
		txtNomeCNPJ.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				zeraPreencheTableVendedor();
			}
		});
		painel_Busca.add(txtNomeCNPJ);
		txtNomeCNPJ.setColumns(10);

		rdbtnNome = new JRadioButton("Nome");
		rdbtnNome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				jftCpfBusca.setVisible(false);
				txtNomeCNPJ.setVisible(true);
			}
		});
		buttonGroup.add(rdbtnNome);
		rdbtnNome.setSelected(true);

		rdbtnCpf = new JRadioButton("CPF");
		rdbtnCpf.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jftCpfBusca.setVisible(true);
				txtNomeCNPJ.setVisible(false);
			}
		});
		buttonGroup.add(rdbtnCpf);

		rdbtnCnpj = new JRadioButton("CNPJ");
		rdbtnCnpj.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				jftCpfBusca.setVisible(false);
				txtNomeCNPJ.setVisible(true);
			}
		});
		buttonGroup.add(rdbtnCnpj);
		GroupLayout gl_painelTp_Busca = new GroupLayout(painelTp_Busca);
		gl_painelTp_Busca.setHorizontalGroup(

				gl_painelTp_Busca.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_painelTp_Busca.createSequentialGroup()
						.addGroup(gl_painelTp_Busca.createParallelGroup(Alignment.LEADING)
								.addComponent(rdbtnNome)
								.addComponent(rdbtnCpf)
								.addComponent(rdbtnCnpj))
								.addContainerGap(25, Short.MAX_VALUE))
				);
		gl_painelTp_Busca.setVerticalGroup(
				gl_painelTp_Busca.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_painelTp_Busca.createSequentialGroup()
						.addContainerGap()
						.addComponent(rdbtnNome)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(rdbtnCpf)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(rdbtnCnpj)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		painelTp_Busca.setLayout(gl_painelTp_Busca);

		jtVendedor = new JTable();
		jtVendedor.setModel(modeloTabelaBuscaVendedor);
		jtVendedor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					setVendedor(modeloTabelaBuscaVendedor.getVendedorAt(jtVendedor.getSelectedRow()));
					dispose();
				}
			}
		});
		spVendedo.setViewportView(jtVendedor);
		contentPanel.setLayout(gl_contentPanel);
	}

	private void zeraPreencheTableVendedor() {
		modeloTabelaBuscaVendedor = new ModeloTabelaBuscaVendedor();
		jtVendedor.setModel(modeloTabelaBuscaVendedor);
		jtVendedor.getTableHeader().setReorderingAllowed(false);
		jtVendedor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jtVendedor.setAutoCreateRowSorter(true);
		
		if(rdbtnNome.isSelected()){
			modeloTabelaBuscaVendedor.setPacientes(
					VendedorModel.pegaVendedorByNome(txtNomeCNPJ.getText())
					);
		}else if(rdbtnCpf.isSelected()){
			modeloTabelaBuscaVendedor.setPacientes(
					VendedorModel.pegaVendedorByCpf(jftCpfBusca.getText())
					);
		}else if(rdbtnCnpj.isSelected())
			modeloTabelaBuscaVendedor.setPacientes(
					VendedorModel.pegaVendedorByCnpj(txtNomeCNPJ.getText())
					);
	}
}
